// pages/myOrder/myOrder.js
import profile from '../../profile.js'

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    lang: app.globalData.language,
    index: profile.myOrder,

    // tab 切换相关
    indexClassNav: profile.myOrder.indexClassNav,
    navNum: 0,

    // 预约活动
    eventsList: profile.myOrder.eventsList,

    // 弹窗相关
    popShow: false,
    popTile: '',
    popContent: [],
    popBtn: [],
    popTextarea: false,
    popTextareaInput: '',
    btnId: '',
    popErrorShow: false
  },

  // 弹窗事件
  popBtnTap: function(e) {
    let type = e.detail.id, text = this.data.popTextareaInput, id = this.data.btnId, list = this.data.eventsList, date = this.formatDateTime(new Date());
    if (type == 'comfirmCancel') {
      // 取消预约
      for (let i=0;i<list.length;i++){
        if (id == list[i].id) {
          list.splice(i,1);
        }
      }
    }
    if (type == 'submitComment') {
      // 提交评论
      if (text == '') {
        this.setData({
          popErrorShow: true
        })
        return;
      }
      for (let i=0;i<list.length;i++){
        if (id == list[i].id) {
          list[i].commentDate = date;
          list[i].commentContent = text;
          list[i].state = '已评论';
          list[i].state_en = 'Commented';
          list[i].btnText = '';
        }
      }
    }
    this.setData({
      popShow: false,
      eventsList: list
    })
    profile.myOrder.eventsList = list;
  },

  formatDateTime: function (date) {  
    var y = date.getFullYear();  
    var m = date.getMonth() + 1;  
    m = m < 10 ? ('0' + m) : m;  
    var d = date.getDate();  
    d = d < 10 ? ('0' + d) : d;  
    var h = date.getHours();  
    h=h < 10 ? ('0' + h) : h;  
    var minute = date.getMinutes();  
    minute = minute < 10 ? ('0' + minute) : minute;  
    var second=date.getSeconds();  
    second=second < 10 ? ('0' + second) : second;  
    return y + '/' + m + '/' + d+' '+h+':'+minute+':'+second;  
  },

  // 侦听弹窗输入事件
  popInputTap: function(e) {
    this.setData({
      popErrorShow: false,
      popTextareaInput: e.detail.value
    })
  },

  // 预约活动按钮事件
  eventTap: function(e) {
    let id = e.target.id, type = e.target.dataset.btnType, list = this.data.eventsList;
    let popTitle = '', popText = [], popBtn = [], popTextarea = false;
    if (type == 'cancel') {
      if (app.globalData.language == 'cn') {
        popTitle = '确认取消预约';
        popText = [{
          popTextCenter: true,
          text: '确认取消本次活动预约？'
        }];
        popBtn = [{
          id: 'comfirmCancel',
          text: '确定'
        },{
          id: 0,
          text: '取消'
        }]
      }else{
        popTitle = 'Confirm cancellation';
        popText = [{
          popTextCenter: true,
          text: 'Are you sure you want to cancel the appointment?'
        }];
        popBtn = [{
          id: 'comfirmCancel',
          text: 'comfirm'
        },{
          id: 0,
          text: 'cancel'
        }]
      }
    }
    if (type == 'comment') {
      popTextarea = true;
      if (app.globalData.language == 'cn') {
        popTitle = '写下评论';
        popBtn = [{
          id: 'submitComment',
          text: '确定'
        },{
          id: 0,
          text: '取消'
        }]
      }else{
        popTitle = 'Write a comment';
        popText = [{
          popTextCenter: true,
          text: 'Are you sure you want to cancel the appointment?'
        }];
        popBtn = [{
          id: 'comfirmCancel',
          text: 'comfirm'
        },{
          id: 0,
          text: 'cancel'
        }]
      }
    }
    this.setData({
      popTile: popTitle,
      popContent: popText,
      popBtn: popBtn,
      popTextarea: popTextarea,
      popShow: true,
      btnId: id
    })
  },

  // 课程tab切换事件
  changeClassNav: function (e) {
    let id = e.detail.id, list = this.data.indexClassNav;
    for (let i=0;i<list.length;i++){
      if (list[i].id == id) {
        list[i].onthis = true
      }else{
        list[i].onthis = false
      }
    }
    this.setData({
      indexClassNav: list,
      navNum: e.detail.id
    })
    profile.myOrder.indexClassNav = list;
  },

  // 语言切换事件
  menuTap: function (e) {
    this.setData({
      lang: e.detail.lang
    })
    if (app.globalData.language == 'cn'){
      wx.setNavigationBarTitle({
        title: '我的预约'
      })
    }else{
      wx.setNavigationBarTitle({
        title: 'My order'
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.tabIndex);
    let index = options.tabIndex, list = this.data.indexClassNav;
    for (let i=0;i<list.length;i++){
      if (list[i].id == index) {
        list[i].onthis = true
      }else{
        list[i].onthis = false
      }
    }
    this.setData({
      indexClassNav: list,
      navNum: index
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      lang: app.globalData.language
    })
    if (app.globalData.language == 'cn'){
      wx.setNavigationBarTitle({
        title: '我的预约'
      })
    }else{
      wx.setNavigationBarTitle({
        title: 'My order'
      })
    }
  },
})